package com.actimize.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableComponent;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * This class represents the basic "account" object in AppFuse that allows for authentication
 * and user management.  It implements Acegi Security's UserDetails interface.
 *
 */
@Entity
@Table(name = "account")
@Searchable
@XmlRootElement
public class Account extends BaseObject implements Serializable {
    private static final long serialVersionUID = 3832626162173359412L;

    private Long accountID;
    private Long userID;
    private String sortCode;                    // required
    private Double balance;                    // required
    private String type;
    private String name;
    

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Account() {
    }

    /**
     * Create a new instance and set the username.
     *
     * @param username login name for user.
     */
    public Account(final String accountName) {
        this.name = accountName;
    }

    @Id
    @SearchableId
    public Long getAccountID() {
        return accountID;
    }

    @Column(name = "userID", nullable = false, length = 50, unique = true)
    @SearchableProperty
    public Long getUserID() {
        return userID;
    }

    @Column(name = "sortCode", nullable = false)
    @XmlTransient
    public String getSortCode() {
        return sortCode;
    }


    @Column(name = "balance")
    @XmlTransient
    public Double getBalance() {
        return balance;
    }

    @Column(name = "type", nullable = false, length = 50)
    @SearchableProperty
    public String getType() {
        return type;
    }

    @Column(name = "name", nullable = false, length = 50)
    @SearchableProperty
    public String getName() {
        return name;
    }


    public void setAccountID(Long id) {
        this.accountID = id;
    }

    public void setUserID(Long userID) {
        this.userID = userID;
    }

    public void setSortCode(String sortCode) {
        this.sortCode = sortCode;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Account)) {
            return false;
        }

        final Account account = (Account) o;

        return !(accountID != null ? !accountID.equals(account.getAccountID()) : account.getAccountID() != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (accountID != null ? accountID.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
                .append("accountID", this.accountID)
                .append("userID", this.userID)
                .append("balance", this.balance)
                .append("type", this.type)
                .append("name", this.name);

        return sb.toString();
    }
}
